package com.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Component
public class RestTemplateUtil {


    @Autowired
    private RestTemplate restTemplate;

    /**
     * @param str 参数  for example :"{\"[]\":{\"Permit_User_PermitList\":{\"State\":1,\"PermitType\":1,\"UserID\":60001,\"@column\":\"CityCaption\"},\"page\":0,\"count\":10000}}";
     * @return
     */
    public ResponseEntity<String> restTemplatePostByGetInfo(String str, String jsonId) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Cookie", jsonId);
        HttpEntity httpEntity = null;
        try {
            //参数
            httpEntity = new HttpEntity(str, headers);
        } catch (Exception e) {
            log.error("111111111", e);
        }
        ResponseEntity<String> responseEntity = null;
        try {
            String url = "http://cricinapi-dev.cricwifi.com/get";
            responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        } catch (Exception e) {
            log.error("2222", e);

        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            log.error(" 333333333", responseEntity);
            return null;
        }


        return responseEntity;
    }


    public ResponseEntity<String> restTemplatePostByInsertInfo(String str, String jsonId) {


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Cookie", jsonId);
        HttpEntity httpEntity = null;
        try {
            //参数
            httpEntity = new HttpEntity(str, headers);
        } catch (Exception e) {
            log.error("111111111", e);
        }
        ResponseEntity<String> responseEntity = null;
        try {
            String url = "http://cricinapi-dev.cricwifi.com/post";
            responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        } catch (Exception e) {
            log.error("2222", e);

        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            log.error(" 333333333", responseEntity);
            return null;
        }


        return responseEntity;

    }

    public ResponseEntity<String> restTemplatePostByUpdateInfo(String str, String jsonId) {


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Cookie", jsonId);
        HttpEntity httpEntity = null;
        try {
            //参数
            httpEntity = new HttpEntity(str, headers);
        } catch (Exception e) {
            log.error("111111111", e);
        }
        ResponseEntity<String> responseEntity = null;
        try {
            String url = "http://cricinapi-dev.cricwifi.com/put";
            responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        } catch (Exception e) {
            log.error("2222", e);

        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            log.error(" 333333333", responseEntity);
            return null;
        }


        return responseEntity;
    }
}
